﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace Stats
{
    class GrafRoky : IGraf
    {
        public override string text { get; set; }
        public override List<string> obdobi{ get;set;}

        public GrafRoky()
        {
            text = "Roky";
        }

        public override List<KeyValuePair<string, int>> GetDataKontext(string obdobi)
        {
            List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>();
            List<Trzba> values = NacisRoky();
            foreach (Trzba value in values)
                valueList.Add(new KeyValuePair<string, int>(value.Date.ToString("yyyy"), value.Penize));
            return valueList;
        }

        /// <summary>
        /// Secte trzby ze vsech tabulek
        /// </summary>
        /// <returns></returns>
        private List<Trzba> NacisRoky()
        {
            List<string> tablesVracene = DataLib.Settings.databases.dbPujcky.QueryGetListString("SELECT table_name from information_schema.tables WHERE table_name LIKE 'Vracene%'");
            List<Trzba> trzby = new List<Trzba>();
            foreach (string tableVracene in tablesVracene)
            {
                int trzba = DataLib.Settings.databases.dbPujcky.QueryGetInt("SELECT SUM(cena) FROM " + tableVracene);
                DateTime date = DateTime.ParseExact(tableVracene.Replace("Vracene", ""), "yyyy", CultureInfo.InvariantCulture);
                trzby.Add(new Trzba(date, trzba));
            }

            int trzbaNevracene = DataLib.Settings.databases.dbPujcky.QueryGetInt("SELECT SUM(cena) FROM nevracene");
            trzby[trzby.Count - 1].Penize += trzbaNevracene;
            return trzby;
        }
    }
}
